﻿using System.Collections.Generic;
using Framework.SqlDataAccess.Manager;
using Framework.SqlDataAccess.Model;
using System;
using WebSiteFramework;

/// <summary>
/// Summary description for BasePage
/// </summary>
public abstract class BasePage : PageBase
{
    #region -= Factory =-
    public T CreateController<T>()
        where T : Framework.SqlDataAccess.Controller.DALCBase
    {
        return CreateController<T>(null, null);
    }

    public T CreateController<T>(AbstractModel model)
        where T : Framework.SqlDataAccess.Controller.DALCBase
    {
        return CreateController<T>(model, null);
    }

    public virtual T CreateController<T>(AbstractModel model, Framework.SqlDataAccess.Controller.DALCBase parent)
        where T : Framework.SqlDataAccess.Controller.DALCBase
    {
        var result = Factory.Controller<T>(parent);
        if (result is Framework.SqlDataAccess.Controller.ICompany)
        {
            //(result as Framework.SqlDataAccess.Controller.ICompany).CompanyID = CompanyID;
            //(result as Framework.SqlDataAccess.Controller.ICompany).UpdateBy = UserID;
            (result as Framework.SqlDataAccess.Controller.ICompany).UpdateDate = DateTime.Now;
        }
        return result;
    }

    public M CreateManager<M>()
        where M : IManager
    {
        var result = Factory.Manager<M>();
        if (result.DALCInfo is Framework.SqlDataAccess.Controller.ICompany)
        {
            //(result.DALCInfo as Framework.SqlDataAccess.Controller.ICompany).CompanyID = CompanyID;
            //(result.DALCInfo as Framework.SqlDataAccess.Controller.ICompany).UpdateBy = UserID;
            (result.DALCInfo as Framework.SqlDataAccess.Controller.ICompany).UpdateDate = DateTime.Now;
        }
        return result;
    }

    #endregion

    public abstract SitePage CurrentSitePage { get; }

    public virtual List<PageLink> MetaDataList
    {
        get
        {
            return new List<PageLink>
                   {
                       new PageLink(SitePage.Main, "Головна", "Default.aspx"),
                       new PageLink(SitePage.News, "Новини", "News.aspx"),
                       new PageLink(SitePage.LowArticle, "Законодавство", "Article.aspx"),
                       new PageLink(SitePage.Services, "Послуги", "Services.aspx"),
                       new PageLink(SitePage.Advice, "Консультації", "Advices.aspx"),
                       new PageLink(SitePage.AboutUs, "Про нас", "AboutUs.aspx"),
                   };
        }
    }

    public abstract List<Crumb> Breadcrumbs { get; }

    protected override void InitPathToControls(List<string> pathList)
    {
        base.InitPathToControls(pathList);
        pathList.Add("~/Controls/AdminControls");
    }

    protected T GetRequestParam<T>(string paramName)
    {
        string v = Request[paramName];
        return GetValueEx<T>(string.IsNullOrEmpty(v) || v == "null" ? null : v);
    }

    private T GetValueEx<T>(object source)
    {
        Type utype = Nullable.GetUnderlyingType(typeof(T));
        Type etype = (utype ?? typeof(T)).BaseType != typeof(Enum) ? null : Enum.GetUnderlyingType(utype ?? typeof(T));
        if (etype != null)
            return (T)Enum.ToObject(utype ?? typeof(T), source);
        return (T)Convert.ChangeType(source, utype ?? typeof(T));
    }
}